package com.qf.listener;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Slf4j
public class PreSendQueueListener {

    @Autowired
    private FilterChainManager filterChainManager;

    @RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)
    public void messageHandler(StandardSubmit standardSubmit, Channel channel, Message message){

        log.info("[策略模块 - 接收短信数据] 接收的短信对象为： StandardSubmit={}",standardSubmit);

        //1. 调用service  对短信对象进行策略处理
        filterChainManager.execute(standardSubmit);

        //2. 对消息进行手动ack
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
            log.info("[策略模块 - 接收短信数据执行失败]  短信对象：StandardSubmit={}",standardSubmit);
        }

    }
}
